package com.ikangtai.shecare.common.db.sync;

import android.content.Context;

import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.db.DBManager;
import com.ikangtai.shecare.common.eventBusMsg.sync.SyncMACAddressForUnbindMsg;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.OKHttpUtil;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONException;
import org.json.JSONObject;
import org.simple.eventbus.EventBus;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 添加删除任务列表，每天要做的任务有哪些。
 */
public class SyncMACAddressForUnbind {
    private Context context;
    private SyncMACAddressForUnbindMsg syncMACAddressForUnbindMsg;

    public SyncMACAddressForUnbind(Context context, SyncMACAddressForUnbindMsg syncMACAddressForUnbindMsg) {
        this.context = context;
        this.syncMACAddressForUnbindMsg = syncMACAddressForUnbindMsg;
        EventBus.getDefault().register(this);
    }

    public void syncMACAddressForUnbindWithNetwork() {
        AppLog.i("MyDeviceActivity 开始 syncMACAddressForUnbindWithNetwork()！");
        final DBManager dbManager = new DBManager(context);

        String macAddress = dbManager.getUnSyncedMacAddress(App.userName);

        if (macAddress == null) {
            AppLog.i("MyDeviceActivity syncMACAddressForUnbindWithNetwork 41！");
            EventBus.getDefault().post(syncMACAddressForUnbindMsg);
        } else {
            long time = System.currentTimeMillis();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = new Date(time);
            String dateStr = format.format(date1);
            OKHttpUtil okHttpClient = new OKHttpUtil(context);
            String appendUrl = "hardware/bindingThermometer.json";
            try {
                okHttpClient.add("authToken", App.authToken);
                okHttpClient.add("MACAddress", macAddress);
                okHttpClient.add("OSType", syncMACAddressForUnbindMsg.getOSType());
                okHttpClient.add("location", "");
                okHttpClient.add("bindingTime", dateStr);
                okHttpClient.postAsync(appendUrl, okHttpClient.new BaseCallBack() {

                    @Override
                    public void onFailure(Request request, IOException e) {
                        super.onFailure(request, e);
                        AppLog.i("MyDeviceActivity syncMACAddressForUnbindWithNetwork 61！");
                        EventBus.getDefault().post(syncMACAddressForUnbindMsg);
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        int respCode = 0;
                        try {
                            JSONObject respJSONObj = new JSONObject(response.body().string());
                            respCode = respJSONObj.getInt("code");
                            if (200 == respCode) {
                                AppLog.i("upload registerUserInfo onResponse success!");
                                dbManager.updateSyncMACAddressFlag(App.userName);
                            } else if (respCode == App.HTTP_RESP_TOKEN_TIME_OUT) {
                                AppLog.i("MyDeviceActivity syncMACAddressForUnbindWithNetwork 时 Token过期，获取新Token 再 Sync！");
                                OKHttpUtil.setNewToken();
                                syncMACAddressForUnbindWithNetwork();
                            } else {
                                AppLog.i("upload registerUserInfo onResponse respCode failed! is:" + respCode);
                            }
                        } catch (JSONException e) {
                            AppLog.i("upload registerUserInfo onResponse failed!");
                            e.printStackTrace();
                        }
                        if (respCode != App.HTTP_RESP_TOKEN_TIME_OUT) {
                            AppLog.i("MyDeviceActivity syncMACAddressForUnbindWithNetwork 86！");
                            EventBus.getDefault().post(syncMACAddressForUnbindMsg);
                        }
                    }
                });
            } catch (JSONException e) {
                AppLog.i("MyDeviceActivity syncMACAddressForUnbindWithNetwork 92！");
                EventBus.getDefault().post(syncMACAddressForUnbindMsg);
            }
        }
    }

}
